package edu.xpu.project.record.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.xpu.project.record.entity.AssignBusRecord;
import edu.xpu.project.record.repository.AssignBusRecordRepository;

@Service
@Transactional(readOnly=true)
public class AssignBusRecordServiceImpl implements AssignBusRecordService {
	
	@Autowired
	private AssignBusRecordRepository assignBusRecordRepository;

	@Override
	public AssignBusRecord get(Long id) {
		return assignBusRecordRepository.findOne(id);
	}

	@Transactional(readOnly=false)
	@Override
	public AssignBusRecord saveOrUpdate(AssignBusRecord t) {
		return assignBusRecordRepository.saveAndFlush(t);
	}

	@Transactional(readOnly=false)
	@Override
	public void delete(Long id) {
		assignBusRecordRepository.delete(id);
	}

	@Override
	public Page<AssignBusRecord> getPageByCriterias(String pageNumberStr,
			String pageSizeStr, final AssignBusRecord assignBusRecord) {
		Integer pageNumber = 1;
		Integer pageSize = 10;
		try {
			pageNumber = Integer.parseInt(pageNumberStr);
		} catch (Exception e) {}
		try {
			pageSize = Integer.parseInt(pageSizeStr);
		} catch (Exception e) {}
		Pageable pageable = new PageRequest(pageNumber - 1, pageSize, new Sort(Direction.DESC, "createDate"));
		return assignBusRecordRepository.findAll(new Specification<AssignBusRecord>() {
			@Override
			public Predicate toPredicate(Root<AssignBusRecord> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				if (assignBusRecord == null) {
					return null;
				}
				List<Predicate> predicates = new ArrayList<Predicate>(3);
				if (assignBusRecord.getStatus() != null && assignBusRecord.getStatus() != -1) {
					predicates.add(cb.equal(root.get("status").as(Integer.class), assignBusRecord.getStatus()));
				}
				if (assignBusRecord.getAssignDate() != null) {
					predicates.add(cb.equal(root.get("assignDate").as(Date.class), assignBusRecord.getAssignDate()));
				}
				if (assignBusRecord.getBusSchedule() != null) {
					if (assignBusRecord.getBusSchedule().getStartStation() != null && assignBusRecord.getBusSchedule().getStartStation() != -1) {
						predicates.add(cb.equal(root.get("busSchedule").get("startStation").as(Integer.class), assignBusRecord.getBusSchedule().getStartStation()));
					}
				}
				
				if (predicates.size() == 0) {
					return null;
				}
				
				Predicate[] predArr= new Predicate[predicates.size()];
				return cb.and(predicates.toArray(predArr));
			}
		}, pageable);
	}

	@Override
	public List<AssignBusRecord> getRecordsByStatus(Integer status) {
		return assignBusRecordRepository.findByStatus(status);
	}

	@Override
	public List<AssignBusRecord> getRecordsByBusScheduleIdAndTime(Long scheduleId, String dateStr) {
		return assignBusRecordRepository.getRecordsByBusScheduleIdAndTime(scheduleId, dateStr);
	}

}
